package com.aaa.controller;

import com.aaa.entity.PMS_Product;
import com.aaa.mapper.PMS_ProductMapper;
import com.aaa.rabbitmq.RabbitMQConfig;
import com.aaa.service.PMS_ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("pms_product")
@Slf4j
public class PMS_ProductController {
    @Resource
    PMS_ProductService pms_productService;

    @Resource
    PMS_ProductMapper pms_productMapper;

    @Resource
    RabbitTemplate rabbitTemplate;

    @RequestMapping("pms_productList")
    public List<PMS_Product> cms_subjectList(){
        return pms_productService.list();
    }

    @RequestMapping("pms_productNameLike")
    public List<PMS_Product> pms_productNameLike(String name){
        return pms_productService.pms_productNameLike(name);
    }

    @ResponseBody
    @RequestMapping("pms_productStateList")
    public List<PMS_Product> pms_productStateList(String brandName){
        System.out.println(brandName+"模糊查询");
        return pms_productService.pms_productStateList(brandName);
    }
    //批量删除
    @ResponseBody
    @RequestMapping("pms_productDeleteStr")
    public int pms_productDeleteStr(String str){

        System.out.println(str+"批量删除");
        String[] eid=str.split(",");
//        List<String> List=new ArrayList<>();
        for (int i=0;i<eid.length;i++) {
            String id = eid[i];
            pms_productService.pms_productDeleteStr(id);
            pms_productService.pms_productDeleteStrTime(id);
        }
        return 1;
    }
    //参加活动弹框查询
    @ResponseBody
    @RequestMapping("pms_productState")
    public List<PMS_Product> pms_productState(){
        return pms_productService.pms_productState();
    }
    //添加活动修改状态参加活动
    @ResponseBody
    @RequestMapping("pms_productUpdateState")
    public int pms_productUpdateState(String str){
        String[] eid=str.split(",");
        for (int i=0;i<eid.length;i++) {
            String id = eid[i];
            pms_productService.pms_productUpdateState(id);
        }
        return 1;
    }

    // private String promotionStartTime;
    // private String promotionEndTime;
    // private Integer promotionPerLimit;
    //修改活动内容
    @ResponseBody
    @RequestMapping("pms_productUpdate")
    public int pms_productUpdate(PMS_Product pms_product){

        PMS_Product pms_product1=pms_productMapper.selectPms(pms_product.getId());

        //pms_product.setPromotionVersion(pms_product1.getPromotionVersion() + 1);

        if (!pms_product.getPromotionStartTime().equals(pms_product1.getPromotionStartTime())){
            pms_product.setStart(true);
            rabbitTemplate.convertAndSend(RabbitMQConfig.PMS_PRODUCT_QUEUE,pms_product, message -> {
                //获取过期时间promotionStartTime
                Long expirationTime= pms_product.getPromotionStartTime().getTime() - new Date().getTime();
                log.info(">>>过期"+expirationTime);
                MessageProperties properties = message.getMessageProperties();
                properties.setExpiration(expirationTime.toString());
                return message;
            });
        }
       return pms_productService.pms_productUpdate(pms_product);
    }
}
